﻿using AuSys.Services.App.FileStorage;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AuSys.Test.TestSupport.Fakes
{
    public class FakeFileStorage : IFileStorage
    {
        public StorageProvideType ProvideType => StorageProvideType.Stream;

        private readonly Dictionary<string, MemoryStream> filesInMem = [];

        public bool Delete(string pathName, out string? errmsg)
        {
            throw new NotImplementedException();
        }

        public string FullUrl(string pathName)
        {
            throw new NotImplementedException();
        }

        public string GetUrlBase()
        {
            throw new NotImplementedException();
        }

        public Stream? Read(string pathName)
        {
            if(filesInMem.TryGetValue(pathName, out var stream))
            {
                stream.Seek(0, SeekOrigin.Begin);
                return stream;
            }
            return null;
        }

        public bool Save(Stream s, string pathName, out string? errmsg)
        {
            var memStream = new MemoryStream();
            s.CopyTo(memStream);
            s.Flush();
            if (!filesInMem.TryAdd(pathName, memStream))
            {
                errmsg = "文件名已存在";
                return false;
            }
            errmsg = null;
            return true;
        }

        public int TotalCount()
            => filesInMem.Count;
    }
}
